import {
  addSysUser,
  execInsert,
  execSelect, findUserById,
  listByPage,
  listByPageCasUserSync, queryAllUnInitSysuserCount,
  sayHi, syncTmpUserToSysUser,
  testData
} from "@/services/systeminitApi";

export default {
  namespace: 'systeminit',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    dataCasUserSync: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    /**
     * 测试用接口
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *sayHi({ payload }, { call, put }) {
      const response = yield call(sayHi, payload);
      return response;
    },

    /**
     * 分页获取数据
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *testData({ payload }, { call, put }) {
      const response = yield call(testData, payload);
      yield put({
        type: 'cb4testData',
        payload: response,
      });
    },

    *listByPage({ payload }, { call, put }) {
      const response = yield call(listByPage, payload);
      yield put({
        type: 'cb4listAllByPage',
        payload: response,
      });
    },

    *listByPageCasUserSync({ payload }, { call, put }) {
      const response = yield call(listByPageCasUserSync, payload);
      yield put({
        type: 'cb4listByPageCasUserSync',
        payload: response,
      });
    },
    /**
     * 查询
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *execSelect({ payload }, { call, put }) {
      const response = yield call(execSelect, payload);
      return response;
    },

    /**
     * 查询未同步到系统用户的cas用户数量
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *queryAllUnInitSysuserCount({ payload }, { call, put }) {
      const response = yield call(queryAllUnInitSysuserCount, payload);
      return response;
    },

    /**
     * 新增
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *execInsert({ payload }, { call, put }) {
      const response = yield call(execInsert, payload);
      return response;
    },

    /**
     * 根据id,查询临时表中的cas用户数据
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *findUserById({ payload }, { call, put }) {
      const response = yield call(findUserById, payload);
      return response;
    },

    /**
     * 新增临时用户
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *addSysUser({ payload }, { call, put }) {
      const response = yield call(addSysUser, payload);
      return response;
    },

    /**
     * 将临时库中的用户数据,写入正式库当中
     * @param payload
     * @param call
     * @param put
     * @returns {Generator<*, *, *>}
     */
    *syncTmpUserToSysUser({ payload }, { call, put }) {
      const response = yield call(syncTmpUserToSysUser, payload);
      return response;
    },
  },

  reducers: {
    cb4listAllByPage(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    cb4listByPageCasUserSync(state, action) {
      return {
        ...state,
        dataCasUserSync: action.payload.data,
      };
    },
    cb4testData(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
  },
};
